package carrier.service;


import carrier.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.entity.*;
import com.example.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageService {
    @Resource
    TruckMapper truckMapper;
    @Resource
    CarrierDriverMapper carrierDriverMapper;

    private static final String SOURCE = "carrier";
    private static final Logger LOG = LoggerFactory.getLogger(MessageService.class);
    @Resource
    TruckOrderMapper truckOrderMapper;
    @Resource
    ShippingOrderMapper shippingOrderMapper;
    @Resource
    KafkaTemplate<Long, Message> template;

    public void updateTruck(Long truckId, Integer state) {
        Truck t = truckMapper.selectById(truckId);
        t.setState(state);
        truckMapper.updateById(t);
    }

    public void updateDriver(Integer state, Long carrierId, Long driverId) {
        CarrierDriver cd = new CarrierDriver(carrierId,driverId);
        cd.setState(state);
        carrierDriverMapper.update(cd,new UpdateWrapper<CarrierDriver>().eq("driver_id",driverId));
    }

    public void busy(Message m) {
        TruckOrder t = truckOrderMapper.selectById(m.getOrderId());
        try{
            updateTruck(t.getTruckId(),1);
            updateDriver(1,t.getCarrierId(),t.getDriverId());
            m.setStatus("DISPATCH SUCCESS");
        }catch (Exception e){
            m.setStatus("FAIL");
        }
        m.setSource(SOURCE);
        template.send("carrier-orders", m.getOrderId(), m);
        LOG.info("Sent: {}", m);
    }

    public void free(Message m) {
        ShippingOrder s = shippingOrderMapper.selectById(m.getOrderId());
        List<TruckOrder> tl = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
        try{
            tl.forEach(t -> {
                updateTruck(t.getTruckId(),0);
                updateDriver(0,s.getCarrierId(),t.getDriverId());
            });
            m.setStatus("ACCEPT");
        }catch (Exception e){
            m.setStatus("REJECT");
        }
        m.setSource(SOURCE);
        template.send("carrier-orders", m.getOrderId(), m);
        LOG.info("Sent: {}", m);

    }

    public void rollback(Message m) {
        if (m.getStatus().equals("ROLLBACK") && !m.getSource().equals(SOURCE)) {
            ShippingOrder s = shippingOrderMapper.selectById(m.getOrderId());
            List<TruckOrder> tl = truckOrderMapper.selectList(new QueryWrapper<TruckOrder>().eq("order_id",s.getId()));
            tl.forEach(t -> {
                updateTruck(t.getTruckId(),1);
                updateDriver(1,s.getCarrierId(),t.getDriverId());
            });
        }
    }
}
